/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


/**
 * @file
 * @brief Generic functions to manage a rectangle.
 */


#ifndef PLANET_WARS_2D_RT_CORE_RECTANGLE_GENERIC_H
#define PLANET_WARS_2D_RT_CORE_RECTANGLE_GENERIC_H


#include "PlanetWars2dRT-core/utils/position_2d_generic.h"


/**
 * @brief Define a rectangle of specified type.
 * @param type A type
 */
#define DEFINE_RECTANGLE_STRUCT(type)			\
  struct rectangle_##type				\
  {							\
    position_2d_##type position1;			\
    position_2d_##type position2;			\
  };							\
  typedef struct rectangle_##type rectangle_##type;

/**
 * @brief Returns true if 2 rectangles are equal, otherwise false.
 * @param rectangle1 A first rectangle
 * @param rectangle2 A second rectangle
 * @return True if 2 rectangles are equal, otherwise false
 */
#define RECTANGLE_EQUALS(rectangle1, rectangle2)			\
  ( POSITION_2D_EQUALS((rectangle1).position1, (rectangle2).position1)	\
    &&									\
    POSITION_2D_EQUALS((rectangle1).position2, (rectangle2).position2) )

#define RECTANGLE_GET_WIDTH(a_rectangle)			\
  ( (a_rectangle).position1.x < (a_rectangle).position2.x	\
    ? (a_rectangle).position2.x - (a_rectangle).position1.x	\
    : (a_rectangle).position1.x - (a_rectangle).position2.x)

#define RECTANGLE_GET_HEIGHT(a_rectangle)			\
  ( (a_rectangle).position1.y < (a_rectangle).position2.y	\
    ? (a_rectangle).position2.y - (a_rectangle).position1.y	\
    : (a_rectangle).position1.y - (a_rectangle).position2.y)

#define RECTANGLE_GET_TOP_LEFT_POSITION_X(a_rectangle)		\
  ( (a_rectangle).position1.x < (a_rectangle).position2.x	\
    ? (a_rectangle).position1.x : (a_rectangle).position2.x )

#define RECTANGLE_GET_BOTTOM_RIGHT_POSITION_X(a_rectangle)	\
  ( (a_rectangle).position1.x > (a_rectangle).position2.x	\
    ? (a_rectangle).position1.x : (a_rectangle).position2.x )

#define RECTANGLE_GET_TOP_LEFT_POSITION_Y(a_rectangle)		\
  ( (a_rectangle).position1.y < (a_rectangle).position2.y	\
    ? (a_rectangle).position1.y : (a_rectangle).position2.y )

#define RECTANGLE_GET_BOTTOM_RIGHT_POSITION_Y(a_rectangle)	\
  ( (a_rectangle).position1.y > (a_rectangle).position2.y	\
    ? (a_rectangle).position1.y : (a_rectangle).position2.y )


#endif
